package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author zhmwi
 * @Date 2021/9/4
 * @Dersciption 客户
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    /**
     * 分页查询客户列表
     * @param page
     * @param rows
     * @param customerName
     * @return
     */
    @PostMapping("/list")
    public Map<String,Object> getCustomerList(Integer page,Integer rows,String customerName){
        Map result = new HashMap();
        PageInfo<Customer> pageInfo = customerService.getCustomerList(page,rows,customerName);
        long total = pageInfo.getTotal();
        List<Customer> customerList = pageInfo.getList();
        result.put("total",total);
        result.put("rows",customerList);
        return result;
    }

    /**
     * 客户的添加或修改
     * @param customer
     * @return
     */
    @PostMapping("/save")
    public ServiceVO save(Customer customer){
        if (customer.getCustomerId() != null){
            //修改
            Boolean isUpdate = customerService.update(customer);
            if (isUpdate){
                return new ServiceVO(100,"请求成功");
            }
        }else{
            //新增
            Boolean isSave = customerService.save(customer);
            if (isSave){
                return new ServiceVO(100,"请求成功");
            }
        }
        return new ServiceVO(300,"请求失败");
    }

    @PostMapping("/delete")
    public ServiceVO delete(String ids){
        Boolean isDelete = customerService.delete(ids);
        if (isDelete){
            return new ServiceVO(100,"请求成功");
        }
        return new ServiceVO(300,"请求失败");
    }
}
